package com.bluesmart.mia;

import java.util.ArrayList;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;

import android.os.Bundle;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BabyProfileTempActivity extends BaseActivity implements OnClickListener{

	ArrayList<JSONObject> mArray = new ArrayList<JSONObject>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_baby_profile_milk);

		((TextView) findViewById(R.id.center)).setText(getString(R.string.temperature0));
		((Button) findViewById(R.id.right)).setCompoundDrawablesWithIntrinsicBounds(0, 0,R.drawable.a01, 0);
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);

		String value[] = {"35","36","37","38","39","40","41","42"};
		WheelView number = (WheelView)findViewById(R.id.pick0);
        number.setViewAdapter(new ArrayWheelAdapter<String>(this, value,Color.parseColor("#FFFFFF")));
        
		number = (WheelView)findViewById(R.id.pick1);
        number.setViewAdapter(new ArrayWheelAdapter<String>(this, value,Color.parseColor("#FFFFFF")));
        
        findViewById(R.id.pick2).setVisibility(View.GONE);
	}

	@Override
	public void onClick(View arg0) {
		if(R.id.left == arg0.getId()) finish();
		else
		{
			if(((WheelView)findViewById(R.id.pick0)).getCurrentItem() > ((WheelView)findViewById(R.id.pick1)).getCurrentItem())
			{
				ToastUtil.showToastWithAlertPic(R.string.err_data);
				return;
			}
			
			String temp =  String.valueOf(((WheelView)findViewById(R.id.pick0)).getCurrentItem() + 35)
					+ "-" + String.valueOf(((WheelView)findViewById(R.id.pick1)).getCurrentItem() + 35);
			
			Intent intent = new Intent();
			intent.putExtra("value",temp);
			this.setResult(RESULT_OK, intent);

			sendRequest(temp);
		}
	}
	
	private void sendRequest(String milkId) {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {

				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000")) {
					finish();
					ToastUtil.showToastWithAlertPic(R.string.success);
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else 
				{
				}
			}
		};
		
		JSONObject obj = ((DemoApp)getApplicationContext()).getSeletedBaby();
		
		String username = PreferencesUtils.getString(this, "username");
		String password = PreferencesUtils.getString(this, "password");
		if(username == null || username.length() == 0)
		{
			username = PreferencesUtils.getString(this, "username_tmp");
			password = PreferencesUtils.getString(this, "password_tmp");
		}

		JSONObject params = new JSONObject();
		params.put("mia_un", username);
		params.put("pwd", password);
		params.put("babyid", obj.getString("babyid"));
		params.put("handType", "addpreferences");
		params.put("bottleid", getIntent().getStringExtra("bottle"));
		params.put("milkid", getIntent().getStringExtra("id"));
		params.put("drinktemp", milkId);
		
		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/babyInfoHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
}
